package com.tronsis.library.activity;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.view.ViewCompat;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;

import com.tronsis.library.R;
import com.tronsis.library.permission.PermissionActivity;
import com.tronsis.library.widget.LoadingProgressDialog;

import java.lang.reflect.Method;

/**
 * @author scofield@tronsis.com
 * @date 2018/12/3 10:23
 */
public class BaseActivity extends PermissionActivity {
    protected Context mContext;
    private LoadingProgressDialog mLoadingProgressDialog;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = this;
        Log.w("BaseActivity", this.toString());

//        setupSystemBottomNavigationBar(getWindow());
    }

    protected void setStatusBarUpperAPI21() {
        Window window = getWindow();
        //设置透明状态栏,这样才能让 ContentView 向上
        window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);

        //需要设置这个 flag 才能调用 setStatusBarColor 来设置状态栏颜色
        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);

        ViewGroup mContentView = (ViewGroup) findViewById(Window.ID_ANDROID_CONTENT);
        View mChildView = mContentView.getChildAt(0);
        if (mChildView != null) {
            //注意不是设置 ContentView 的 FitsSystemWindows, 而是设置 ContentView 的第一个子 View . 使其不为系统 View 预留空间.
            ViewCompat.setFitsSystemWindows(mChildView, false);
        }
    }

    protected void showLoadingDialog(String str) {
        if (mLoadingProgressDialog == null) {
            mLoadingProgressDialog = new LoadingProgressDialog(this, R.style.CustomProgressDialog);
            mLoadingProgressDialog.show(str);
        } else {
            mLoadingProgressDialog.show(str);
        }

    }

    protected void dismissLoadingDialog() {
        if (mLoadingProgressDialog != null) {
            mLoadingProgressDialog.dismiss();
        }
    }


    public void setupSystemBottomNavigationBar(Window window) {
        if (window == null) {
            return;
        }

        try {
            Method method = window.getClass().getMethod("setNavigationBarColorOfNubia", int.class);
            if (method != null) {
                method.invoke(window, Color.BLACK);
            }
        } catch (Exception ex) {
            // do nothing
        }
    }

}
